import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:icooker/configs/app_configs.dart';
import 'package:icooker/configs/mock_datas.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:kasax/kasax.dart';

/// 引导页
class GuidePage extends StatefulWidget {
  const GuidePage({Key? key}) : super(key: key);

  @override
  _GuidePageState createState() => _GuidePageState();
}

class _GuidePageState extends State<GuidePage> {
  bool showTipButton = false;

  @override
  Widget build(BuildContext context) {
    print("GuidePage init...");
    return Scaffold(
      body: Container(
        child: Stack(
          children: [
            PageView.builder(
              onPageChanged: (index) {
                setState(() {
                  if (index == GUIDE_IMAGE_LIST.length - 1) {
                    showTipButton = true;
                  } else {
                    showTipButton = false;
                  }
                });
              },
              itemBuilder: (context, index) {
                return Container(
                  child: CachedNetworkImage(
                    fit: BoxFit.cover,
                    imageUrl: GUIDE_IMAGE_LIST[index],
                    placeholder: (
                      BuildContext context,
                      String url,
                    ) =>
                        Container(
                      color: Color(0xFFD6D6D6),
                      child: Center(
                        child: SizedBox(
                          width: 30,
                          height: 30,
                          child: CircularProgressIndicator(
                            color: Colors.blueGrey,
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              },
              itemCount: GUIDE_IMAGE_LIST.length,
            ),
            Positioned(
              child: showTipButton ? buildTipButton() : Container(),
              bottom: 10,
              left: 10,
              right: 10,
            )
          ],
        ),
      ),
    );
  }

  Widget buildTipButton() {
    return Container(
      child: Center(
        child: Opacity(
          opacity: 0.65,
          child: RaisedButton(
            padding: EdgeInsets.only(left: 20, right: 20),
            elevation: 2,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
            color: Colors.grey,
            onPressed: () {
              changeData();
              RouteUtils.pushNamedAndRemoveUntil(context, "/main");
            },
            child: Text(
              "立即体检",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }

  void changeData() async {
    SPUtils.put(AppConfigs.KEY_FIRST_USED_DATA, false);
  }
}
